package com.tensquare.recruit.controller;

import com.tensquare.recruit.pojo.Enterprise;
import com.tensquare.recruit.service.EnterpriseService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/23 15:05
 */
@RestController
@RequestMapping("/enterprise")
@CrossOrigin //跨域处理
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * fetch data by rule id
     *
     * @description  查询全部列表
     * @param
     * @return
     */
    @GetMapping
    public Result findALl(){
        return new Result(true, StatusCode.OK,"查询成功",enterpriseService.findAll());
    }

    /**
     * fetch data by rule id
     *
     * @description  增加
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Enterprise enterprise){
        enterpriseService.add(enterprise);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询企业
     * @param
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findById(id));
    }

    /**
     * fetch data by rule id
     *
     * @description  修改企业
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable String id,@RequestBody Enterprise enterprise){
        enterprise.setId(id);
        enterpriseService.update(enterprise);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID删除企业
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        enterpriseService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询企业
     * @param
     * @return
     */
    @PostMapping("/search")
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findSearch(searchMap));
    }

    /**
     * fetch data by rule id
     *
     * @description  热门企业列表
     * @param
     * @return
     */
    @GetMapping("/search/hotlist")
    public Result hotList(){
        return new Result(true,StatusCode.OK,"查询成功",enterpriseService.hotList());
    }

    /**
     * fetch data by rule id
     *
     * @description  条件 + 分页查询
     * @param
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@PathVariable int page,@PathVariable int size,@RequestBody Map searchMap){
        Page<Enterprise> pageList = enterpriseService.findSearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }
}
